﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelWritter
{
    class SimpleExcelWritter
    {
        public SimpleExcelWritter()
        {
            excelApp = new Excel.Application();
            excelApp.Workbooks.Add();
            workSheet = excelApp.ActiveSheet;            
        }

        private Excel.Application excelApp = null;
        private Excel._Worksheet workSheet= null;

        private int i = 1;
        private char[] c = new char[2]{'A','\0'};
        private const char c_end = 'Z';
        private const char c_begin = 'A';

        private string s
        {
            get
            {
                if (c[1] == '\0')
                    return Convert.ToString(c[0]);
                else
                    return Convert.ToString(c);
            }
        }
        public int intIndex
        {
            get
            {
                return i;
            }
        }
        public string stringIndex
        {
            get
            {
                return s;
            }
        }
        public bool ExcelAppVisible
        {
            get
            {
                return excelApp.Visible;
            }
            set
            {
                excelApp.Visible = value;
            }
        }

        public void Up()
        {
            if (i - 1 >= 1)
                i--;
        }

        public void Down()
        {
            i++;
        }

        public void Right()
        {
            if (c[0] == c_end)
                if (c[1] == c_end)
                    return;
                else
                    if (c[1] == '\0')
                        c[1] = 'A';
                    else
                        c[1]++;
            else
                c[0]++;
        }

        public void Left()
        {
            if (c[0] == c_begin)
                if (c[1] == c_begin)
                    c[1] = '\0';
                else
                    if ((c[0]==c_begin))
                        return;
                    else
                        c[0]--;
            else
                c[1]--;
        }

        public void Write(string value)
        {
            workSheet.Cells[i, s] = value;
        }

        public string Read()
        {
            return Convert.ToString(workSheet.Cells[i, s].Value);
        }

        public void ExcelClose()
        {
            excelApp.Quit();
        }
    }
}
